﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business_Objects;
using ServicesComponent.Repositories;

public partial class QuanLyNhanVien_TheoDoiThiDuaDetails : System.Web.UI.Page
{
    private ITheoDoiThiDuaRepository _TheoDoiThiDuaRepository;
    private INhanVienRepository _NhanVienRepository;
    private ISchoolYearRepository _SchoolYearRepository;
    private IClassRepository _classRepository;
    private static Guid id = Guid.NewGuid();
    private static string action;
    protected void Page_Load(object sender, EventArgs e)
    {

        _TheoDoiThiDuaRepository = FactoryServices.GetServicesTheoDoiThiDua(_TheoDoiThiDuaRepository);
        _SchoolYearRepository = FactoryServices.GetServicesSchoolYear(_SchoolYearRepository);
        _classRepository = FactoryServices.GetServicesClass(_classRepository);
        action = Request.QueryString["ac"].ToString();
        if (IsPostBack == false)
        {
            GetDataToCombobox();
            if (action == "'sua'")
            {
                id = Guid.Parse(Request.QueryString["id"].ToString());
               // lblTitle.Text = "Sửa nhóm thực phẩm";
                FillDataToControl(id);
            }
            if (action == "'them'")
            {
              //  lblTitle.Text = "Thêm mới nhóm thực phẩm";
                ddlNam.SelectedValue = Session["SchoolID"].ToString();
                ddlNam.Enabled = false;
            }
      
        }
        if (action == "'Update'")
        {
            int a = 8;
        }
    }
    private void GetDataToCombobox()
    {
        _NhanVienRepository = FactoryServices.GetServicesNhanVien(_NhanVienRepository);
        ddlGiaoVien.DataSource =
            _NhanVienRepository.GetNhanVienBySchoolId(int.Parse(Session["SchoolID"].ToString()));
        ddlGiaoVien.DataValueField = "Id";
        ddlGiaoVien.DataTextField = "HoTen";
        ddlGiaoVien.DataBind();

        _classRepository = FactoryServices.GetServicesClass(_classRepository);
        ddlLop.DataSource =
            _classRepository.GetClassBySchoolIdAndSchoolYearID(int.Parse(Session["SchoolID"].ToString()), Session["SchoolYearID"].ToString());
        ddlLop.DataValueField = "Id";
        ddlLop.DataTextField = "Name";
        ddlLop.DataBind();

        _SchoolYearRepository = FactoryServices.GetServicesSchoolYear(_SchoolYearRepository);
        ddlNam.DataSource =
            _SchoolYearRepository.GetSchoolYearBySchoolId(int.Parse(Session["SchoolID"].ToString()));
        ddlNam.DataValueField = "Id";
        ddlNam.DataTextField = "Name";
        ddlNam.DataBind();

    }
    void FillDataToControl(Guid Id)
    {
        _SchoolYearRepository = FactoryServices.GetServicesSchoolYear(_SchoolYearRepository);
        //Đổ data lên control theo id get về
        TheoDoiThiDua obj = _TheoDoiThiDuaRepository.Get(id.ToString());
     //   SchoolYear sc = _SchoolYearRepository.Get(obj.SchoolYearID.ToString());
        ddlNam.SelectedValue = obj.SchoolYearID.ToString();
        ddlNam.Enabled = false;
        ddlGiaoVien.SelectedValue = obj.NhanVienID.ToString();
        ddlLop.SelectedValue = obj.ClassID.ToString();
        //txtNam.Value = sc.Name;
        //txtNam.Attributes.Add("readonly", "readonly");
        ddl1.SelectedValue = obj.Thang1;
        ddl2.SelectedValue = obj.Thang2;
        ddl3.SelectedValue = obj.Thang3;
        ddl4.SelectedValue = obj.Thang4;
        ddl9.SelectedValue = obj.Thang9;
        ddl10.SelectedValue = obj.Thang10;
        ddl11.SelectedValue = obj.Thang11;
        ddl12.SelectedValue = obj.Thang12;
        ddlXepLoai.SelectedValue = obj.XepLoai;
    }

    bool ValiDate()
    {
        //if (txtTenNhomThucPham.Value.ToString() == "")
        //{
        //    return false;
        //}
        return true;
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (ValiDate())
        {
            if (action == "'sua'")
            {
                //Update
                _TheoDoiThiDuaRepository.Update(new TheoDoiThiDua(id, Guid.Parse(ddlGiaoVien.SelectedValue.ToString()),
                                                                  Guid.Parse(ddlLop.SelectedValue.ToString()),
                                                                  Guid.Parse(Session["SchoolYearID"].ToString()), ddl1.SelectedValue,
                                                                  ddl2.SelectedValue, ddl3.SelectedValue,
                                                                  ddl4.SelectedValue, ddl9.SelectedValue,
                                                                  ddl10.SelectedValue, ddl11.SelectedValue,
                                                                  ddl12.SelectedValue, ddlXepLoai.SelectedValue, ""));
                Alert.Show("Cập nhật dữ liệu thành công!");
                Alert.ClodePopup();
            }
            if (action == "'them'")
            {
                //AddNew
                _TheoDoiThiDuaRepository.Insert(new TheoDoiThiDua(Guid.NewGuid(), Guid.Parse(ddlGiaoVien.SelectedValue.ToString()),
                                                                  Guid.Parse(ddlLop.SelectedValue.ToString()),
                                                                  Guid.Parse(Session["SchoolYearID"].ToString()), ddl1.SelectedValue,
                                                                  ddl2.SelectedValue, ddl3.SelectedValue,
                                                                  ddl4.SelectedValue, ddl9.SelectedValue,
                                                                  ddl10.SelectedValue, ddl11.SelectedValue,
                                                                  ddl12.SelectedValue, ddlXepLoai.SelectedValue, ""));
                Alert.Show("Cập nhật dữ liệu thành công!");
                Alert.ClodePopup();

            }
        }
        else
        {
            Alert.Show("Thông tin cần nhập chưa đầy đủ !");
        }

    }
}